package org.androidpn.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.androidpn.R;
import org.androidpn.adapter.SearchResultLvAdapter;
import org.androidpn.http.HttpApi;
import org.androidpn.model.CUserInfo;
import org.androidpn.model.NetModel;
import org.androidpn.utils.ConstanceUtil;
import org.androidpn.utils.JsonUtil;
import org.androidpn.utils.ToastUtil;
import org.androidpn.widget.CircleImageView;

import com.android.volley.VolleyError;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

public class SearchResultActivity extends Activity implements OnClickListener, OnItemClickListener {

	private ListView lvSearch;
	private EditText etSearch;
	private ImageView ivSearch;
	private List<CUserInfo> cUserInfos=new ArrayList<CUserInfo>();
	SearchResultLvAdapter adapter ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.search_result_activity);
		lvSearch =(ListView) findViewById(R.id.lv_search_result);
		etSearch =(EditText) findViewById(R.id.et_search);
		ivSearch =(ImageView) findViewById(R.id.iv_search);
		ivSearch.setOnClickListener(this);
		adapter =new SearchResultLvAdapter(this);
		adapter.setcUserInfos(cUserInfos);
		lvSearch.setAdapter(adapter);
		lvSearch.setOnItemClickListener(this);
//		etSearch.add
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		
		case R.id.iv_search:
			SearchFriendListener listener =new SearchFriendListener();
			if(TextUtils.isEmpty(etSearch.getText().toString())){
				ToastUtil.showShortToast(this, "搜索条件不能为空");
			}else{
				if(isMobileNO(etSearch.getText().toString())){
				HttpApi.findFriendByTelephone(etSearch.getText().toString(), listener, listener);
				}else{
					HttpApi.findFriendByTag(etSearch.getText().toString(), listener, listener);
				}
				}
			break;

		default:
			break;
		}
	}
	  class SearchFriendListener implements com.android.volley.Response.Listener<String> ,com.android.volley.Response.ErrorListener{

			@Override
			public void onErrorResponse(VolleyError error) {
				// TODO Auto-generated method stub
				ToastUtil.showShortToast(SearchResultActivity.this, "网络异常");
			}
			@Override
			public void onResponse(String response) {
				// TODO Auto-generated method stub
				NetModel netModel =JsonUtil.parseObject(response, NetModel.class);
				if(netModel.getCode() ==ConstanceUtil.REQUEST_SUCCESS){
					Log.e("wulianshu", "cUserInfosRecommendTag:"+netModel.getContent());
					cUserInfos =JsonUtil.parseArray(netModel.getContent(), CUserInfo.class);
					//更新界面
					adapter.setcUserInfos(cUserInfos);
					adapter.notifyDataSetChanged();
				}else{
					cUserInfos.clear();
					adapter.setcUserInfos(cUserInfos);
					adapter.notifyDataSetChanged();
					ToastUtil.showShortToast(SearchResultActivity.this, netModel.getContent());
				    
				}

			}
		}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		Intent intent =new Intent(this,UserInfoDetailActivity.class);
		intent.putExtra("cuserinfo", cUserInfos.get(arg2));
		startActivity(intent);
		SearchResultActivity.this.finish();
	}
	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$");
//		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}
	  
}
